package com.gzc.web.action;

import java.lang.reflect.InvocationTargetException;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.gzc.common.Const;
import com.gzc.model.Message;
import com.gzc.model.User;
import com.gzc.service.IMessageService;
import com.gzc.service.IUserService;
import com.gzc.util.ActionUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.sun.org.apache.commons.beanutils.BeanUtils;

@Controller("messageAction")
@Scope("prototype")
public class MessageAction extends ActionSupport implements ModelDriven<Message> {
	
	private IUserService userService;	
	private IMessageService messageService;
	private Message msg;
	private int isRead;
	private String con;
	private Integer[] sus;	

	@Override
	public Message getModel() {
		if (msg == null) {
			msg = new Message();
		}
		return msg;
	}
	
	public Integer[] getSus() {
		return sus;
	}
	public void setSus(Integer[] sus) {
		this.sus = sus;
	}	
	public int getIsRead() {
		return isRead;
	}
	public void setIsRead(int isRead) {
		this.isRead = isRead;
	}
	public String getCon() {
		return con;
	}
	public void setCon(String con) {
		this.con = con;
	}
	public Message getMsg() {
		return msg;
	}
	public void setMsg(Message msg) {
		this.msg = msg;
	}
	@Resource
	public void setMessageService(IMessageService messageService) {
		this.messageService = messageService;
	}
	@Autowired
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}
	
	//=========================================
	
	public String addInput(){
		User u = (User)ActionContext.getContext().getSession().get(Const.LOGIN_USER_KEY);
		ActionContext.getContext().put("us", userService.listAllSendUser(u.getId()));
		return SUCCESS;
	}
	
	public String add(){
//		if(atts==null||atts.length<=0) {
//			messageService.add(msg, sus,new AttachDto(false),isEmail);
		messageService.add(msg, sus);
//		} else {
//			String uploadPath = ServletActionContext.getServletContext().getRealPath("upload");
//			uploadPath = "D:\\teach_source\\class2\\j2EE\\document_project\\document01\\WebContent\\upload";
//			messageService.add(msg,sus,new AttachDto(atts,attsContentType,attsFileName,uploadPath),isEmail);
//		}
		ActionUtil.setUrl(Const.MSG_LIST_SEND_URL);
		return ActionUtil.REDIRECT;
	}
	
	public String listReceive() {
		ActionContext.getContext().put("pages", messageService.findReceiveMessage(con, isRead));
		return SUCCESS;
	}
	
	public String listSend(){
		ActionContext.getContext().put("pages", messageService.findSendMessage(con));
		return SUCCESS;
	}
	
	public String show() throws IllegalAccessException,
			InvocationTargetException {
		Message tm = messageService.updateRead(msg.getId(), isRead);
		BeanUtils.copyProperties(msg, tm);
		ActionContext.getContext().put("atts",
				messageService.listAttachmentByMsg(msg.getId()));
		return SUCCESS;
	}
	
}
